﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
/// <summary>
/// 指令的包装类
/// </summary>
public class BPInstruction
{
	public enum InstructionType
	{
		None = 0,

		//创建指令
		Create_Player = 1,		//创建玩家
		Create_Npc = 2,			//创建机器人
		Create_EffectItem = 3,

		//
		SendSceneEvent = 11,			//想逻辑处理发送消息
		//
		Wait = 50,              //等待一段时间
		Kill_Trigger = 51,		//终止触发
		Kill_Instruction = 52,	//终止一个指令

		Open_Trigger = 60,		//开启一个触发

		//
		AttachAI = 80           //添加AI脚本
	}

	protected BPInstructionSet Parent { get; set; }
	protected const int MaxParamLength = 8;


	public int Index { get; set; }
	public InstructionType InstructType { get; set; }
	public BPInstruction_Implement Instruction { get; protected set; }
	public string[] Params { get; set; } = new string[MaxParamLength];
	[JsonIgnore]
	public bool IsEnded { get; protected set; } = false;

	virtual public void Reset(int start)
	{
		Index = start;
		IsEnded = false;
		Instruction = null;
	}

	virtual public void DoStart(BPInstructionSet parent)
	{
		Debug.Log("Start" + Index + this.InstructType);
		this.IsEnded = false;
		this.Parent = parent;
		Instruction = BPInstruction_Implement.Create(this);
		if(null != Instruction)
		{
			Instruction.DoStart(this);
			Instruction.DoUpdate(0, this);
		}
	}

	virtual public void DoUpdate(float deltaTime)
	{
		//Create An Empty Instruction Set This Auto End
		if (null == Instruction)
			IsEnded = true;
		//if Current Instruction Is End Dont Updata
		if (IsEnded != true)
			//return;
			//Update Instruction
			IsEnded =  Instruction.DoUpdate(deltaTime, this);
		if(IsEnded == true)
		{
			//TODO
			Debug.Log("Current " + Index + "End");
			DoEnd();
		}
	}

	virtual public void DoEnd()
	{
		if (null != Instruction)
			Instruction.DoEnd(this);
		IsEnded = true;
	}

	/// <summary>
	/// Get A Instruction By index In Current InstructionSet
	/// </summary>
	/// <param name="index"></param>
	virtual public BPInstruction GetInstructionByIndex(int index)
	{
		return Parent.GetInstructionByIndex(index);
	}
}
